package com.xmkj.bearbiz.member;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.common.BearBean.GuKeBiaoShiBean;
import com.common.BearBean.VipUserInfo;
import com.common.BearMethods.GetUserBiaoShiMethods;
import com.common.BearMethods.GetVipUserDatasMethods;
import com.common.cons.Constans;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.utils.BottomPickerUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.functions.Action1;

@SuppressLint("ValidFragment")
public class BaseInfoFragment extends BaseMvpFragment {
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.iv_go)
    ImageView ivGo;
    @BindView(R.id.rl_tip)
    RelativeLayout rlTip;
    @BindView(R.id.tv_birth)
    TextView tvBirth;
    @BindView(R.id.iv_goo)
    ImageView ivGoo;
    @BindView(R.id.rl_birth)
    RelativeLayout rlBirth;
    @BindView(R.id.et_job)
    EditText etJob;
    @BindView(R.id.et_id_card)
    EditText etIdCard;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_company_name)
    EditText etCompanyName;
    @BindView(R.id.et_zongjiao)
    EditText etZongjiao;
    @BindView(R.id.et_home_brith)
    EditText etHomeBrith;
    @BindView(R.id.et_beizhu)
    EditText etBeizhu;
    @BindView(R.id.et_consultant)
    EditText et_consultant;


    Unbinder unbinder;
    private String deptcode;
    private String vipcardid;
    private VipUserInfo bean;
    private OptionsPickerView tipPicker;
    private int customer_identity_ids;

    @SuppressLint("ValidFragment")
    public BaseInfoFragment(String deptcode, String vipcardid) {

        this.deptcode = deptcode;
        this.vipcardid = vipcardid;
    }

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_info;
    }

    @Override
    protected void onViewCreated(View view) {

    }

    private SaveUserInfoBean submitBean = new SaveUserInfoBean();

    @Override
    protected void doLogicFunc() {
        getBiaoShi();
        getVipUserData();
        attachClickListener(rlTip);
        attachClickListener(rlBirth);
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.SAVE_USER_INFO) && (Boolean) rxKeyEvent.getValue()) {
//                   String baseInfo =   tvTip.getText().toString();
                    if (EmptyUtils.isEmpty(bean)) {
                        submitBean.vipcardid = "";
                        submitBean.deptcode = "";
                    } else {
                        submitBean.vipcardid = bean.vipcardid;
                        submitBean.deptcode = bean.deptcode;
                    }

                  if (customer_identity_ids == 0) {
                        submitBean.customer_identity_ids = "";
                    } else {
                        submitBean.customer_identity_ids = customer_identity_ids + "";
                    }
                    submitBean.consultant = getEditTextStr(et_consultant);
                    submitBean.occupation = getEditTextStr(etJob);
                    submitBean.birthday = getTextViewString(tvBirth);
                    submitBean.idcard = getEditTextStr(etIdCard);
                    submitBean.home_address = getEditTextStr(etAddress);
                    submitBean.company_name = getEditTextStr(etCompanyName);
                    submitBean.faith = getEditTextStr(etZongjiao);
                    submitBean.family_festival = getEditTextStr(etHomeBrith);
                    submitBean.remarks = getEditTextStr(etBeizhu);


                    RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.SAVE_FROM_BASE_INFO, submitBean));

                }
            }
        }));
    }

    private void getBiaoShi() {
        CommonSubscriber<ArrayList<GuKeBiaoShiBean>> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                ArrayList<GuKeBiaoShiBean> bean = (ArrayList<GuKeBiaoShiBean>) o;

                initTipPicker(bean);

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetUserBiaoShiMethods.getInstance().getCustomerIdentity(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID));
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {

        if (view.getId() == rlTip.getId()) {
            if (tipPicker != null) {
                tipPicker.show();
            }
        } else if (view.getId() == rlBirth.getId()) {
            Calendar startTime = Calendar.getInstance();
            int year = startTime.get(Calendar.YEAR);    //获取年
            int month = startTime.get(Calendar.MONTH);   //获取月份，0表示1月份
            int day = startTime.get(Calendar.DAY_OF_MONTH);    //获取当前天数
            startTime.set(year, month, day);
            Calendar endTime = Calendar.getInstance();
            endTime.set(2099, 0, 1);
            Calendar selectTime = Calendar.getInstance();
            MyDatePickerRepaymentDate myDatePickerCallBack = new MyDatePickerRepaymentDate();
            BottomPickerUtils.showDatePicker(context, endTime, startTime, selectTime, myDatePickerCallBack);
        }
    }

    private class MyDatePickerRepaymentDate implements BottomPickerUtils.DatePickerCallback {

        @Override
        public void onDateSelect(Calendar calendar) {
            int year = calendar.get(Calendar.YEAR);    //获取年
            int month = calendar.get(Calendar.MONTH) + 1;   //获取月份，0表示1月份
            int day = calendar.get(Calendar.DAY_OF_MONTH);    //获取当前天数
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int min = calendar.get(Calendar.MINUTE);
            String time = year + "-" + month + "-" + day;
            tvBirth.setText(time);
            System.out.println(year + "----------" + month + "月" + day + "日" + hour + "时" + min + "分钟");
//            tvTime.setText(year + "年" + month + "月" + day + "日" + hour + "时" + min + "分");
//            sendtime = calendar.getTimeInMillis() / 1000 + "";
//            has_choose_time = true;
//            end_time = year+"."+month+"."+day;
        }
    }

    private void getVipUserData() {
        CommonSubscriber<VipUserInfo> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                bean = (VipUserInfo) o;
                if(bean.customer_identity_ids.size()>0){
                    tvTip.setText(bean.customer_identity_ids.get(0)); // 顾客标识
                    customer_identity_ids = 1;
                }

                //初始化底部顾客标识
                et_consultant.setText(bean.consultant);
//                initTipPicker();
                tvBirth.setText(bean.birthday);
                etJob.setText(bean.occupation);
                etIdCard.setText(bean.idcard);
                etAddress.setText(bean.home_address);
                etCompanyName.setText(bean.company_name);
                etZongjiao.setText(bean.faith);
                etHomeBrith.setText(bean.family_festival);
                etBeizhu.setText(bean.remarks);


            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

//        jpushid = JPushInterface.getRegistrationID(this);
        GetVipUserDatasMethods.getInstance().seeVipUser(subscriber, SPUtils.getShareInt(Constans.UID) + "", SPUtils.getShareString(Constans.HASHID), vipcardid, deptcode);
        rxManager.add(subscriber);
    }

    private void initTipPicker(final ArrayList<GuKeBiaoShiBean> bean) {
        List<String> ids = new ArrayList<>();

        if (bean != null && bean.size() > 0) {
            for (int i = 0; i < bean.size(); i++) {
                ids.add(bean.get(i).identity);
            }
        }

        if (bean != null && bean.size() > 0) {
            tipPicker = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {

                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                shopCode = shopBeans.get(options1).lists.get(options2).clientcode;
//                tvCondition.setText(shopBeans.get(options1).lists.get(options2).clientname);
//                typecode = shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeCode;
//                tvGoodsSort.setText(one.get(options1) + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).TypeName + "-" + shopCateGroyBeans.get(options1).lists2.get(options2).lists3.get(options3).TypeName);
            /*        tvProductYear.setText(years.get(options1));
                    year = years.get(options1);*/

                    tvTip.setText(bean.get(options1).identity);
                    customer_identity_ids = bean.get(options1).id;
                }
            }).setSubmitText(getString(R.string.insure)).
                    setCancelText(getString(R.string.cacal)).
                    build();
            tipPicker.setPicker(ids);
        }
    }


    @Override
    public void bindKnife() {
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void setStatusBar() {
        super.setStatusBar();
        m_statusBar.setVisibility(View.GONE);
    }
}
